package com.order.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.order.constant.MessageConstant;
import com.order.constant.StatusConstant;
import com.order.dto.DishDTO;
import com.order.dto.DishPageQueryDTO;
import com.order.entity.Dish;
import com.order.entity.DishFlavor;
import com.order.entity.Setmeal;
import com.order.exception.DeletionNotAllowedException;
import com.order.mapper.DishFlavorMapper;
import com.order.mapper.DishMapper;
import com.order.mapper.SetmealDishMapper;
import com.order.mapper.SetmealMapper;
import com.order.result.PageResult;
import com.order.service.DishService;
import com.order.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Transactional
    public void saveDishWithFlavor(DishDTO dishDTO) {
//        向dish表插入数
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
//        向dish_flavor插入数据
        Long dishId=dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
        dishFlavorMapper.insertBatch(flavors);
        }
    }

    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Transactional
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
           Dish dish= dishMapper.getById(id);
           if(dish.getStatus() == StatusConstant.ENABLE){
               throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
           }
        }

        List<Long> setmealIds=setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds!=null&&setmealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        for (Long id : ids) {
            dishMapper.deleteById(id);

            dishFlavorMapper.deleteByDishId(id);
        }


    }

//根据id查询菜品
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish = dishMapper.getById(id);

        List<DishFlavor> dishFlavors=dishFlavorMapper.getByDishId(id);

        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);

        return  dishVO;

    }
//修改菜品
    @Transactional
    public void update(DishDTO dishDTO) {
//        修改菜品基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

//        删除原有的口味
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
//批量插入口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Transactional
    public void setStatus(Integer status, Long id) {
//        设置菜品起售停售
        Dish dish = dishMapper.getById(id);

        if(status==0){
            dish.setStatus(0);
            List<Long> dishIds=new ArrayList<>();
            dishIds.add(id);
            List<Long> setmealIds=setmealDishMapper.getSetmealIdsByDishIds(dishIds);
            if(setmealIds!=null&&setmealIds.size()>0){
                for (Long setmealId : setmealIds) {
                    Setmeal setmeal=Setmeal.builder()
                            .id(setmealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealMapper.update(setmeal);
                }
            }
        }else {
            dish.setStatus(1);
        }
        dishMapper.update(dish);
    }


    public List<Dish> getByCategoryId(Long categoryId) {
        Dish dish=Dish.builder()
                .categoryId((categoryId))
                .status(StatusConstant.ENABLE)
                .build();
                return dishMapper.getByCategoryId(dish);
    }
}
